package im.yisme.view.ui.animation;

import im.yisme.R;
import im.yisme.view.ui.animation.pojo.HaloPOJO;
import im.yisme.view.ui.util.BitmapAlpha;

import java.util.ArrayList;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.Log;

public class Bloom extends Thread{
	private HaloPOJO bloom;
	private long bestTime;
	private static final float EXTEND = 80;
	private static final int ANIMATION_UPDATE_INTERVAL = 50;
	private static final int PAUSE_POLLING_INTERVAL = 500;
	private static final int ACTIVE_TIME_AFTER=500;
	public static final double DURATION=1000;
	private ArrayList<HaloPOJO> list;
	private ArrayList<Bloom> threadList;
    private boolean pause;
	
    private Bitmap torus;
    private PointF center;
    
    
	public Bloom(long bestTime,PointF center,ArrayList<HaloPOJO> haloArrayList,ArrayList<Bloom> threadList, Resources res) {
		this.bestTime=bestTime;

		torus = BitmapFactory.decodeResource(res, R.drawable.game_halo);
	
		this.bloom = new HaloPOJO(new PointF(center.x,center.y), null, 0);
		this.center = center;
		list = haloArrayList;
		this.threadList= threadList;
		list.add(this.bloom);
		threadList.add(this);
		pause=false;
	}

	public void run() {

	  try {
		  while (System.currentTimeMillis()<bestTime+ACTIVE_TIME_AFTER) {

			while(pause) Thread.sleep(PAUSE_POLLING_INTERVAL);
			
			float extend =  (float) (((DURATION-(bestTime-System.currentTimeMillis())) / DURATION) * EXTEND);
			
			float scale = extend / EXTEND;
			
			if(scale > 1){
				scale = 1;
				extend = EXTEND;
			}
				
			int tempAlpha = (int) (255 * ((DURATION-(bestTime-System.currentTimeMillis())) / DURATION));
			if(scale == 1) tempAlpha = (int) (255-255*(System.currentTimeMillis()-bestTime)/ACTIVE_TIME_AFTER);
			
			if(tempAlpha > 255) tempAlpha = 255;
			if(tempAlpha < 0) tempAlpha = 0;
			bloom.setAlpha(tempAlpha);
			
			
			//Bitmap torusTemp = BitmapAlpha.setAlpha(torus, tempAlpha);
			
			Bitmap torusTemp = zoomIn(torus, scale);
			
			
			bloom.setHalo(torusTemp);
			
			PointF point = new PointF(center.x - extend,
					                  center.y - extend );
			bloom.setHaloPointF(point);
			
			
			
			
			
				Thread.sleep(ANIMATION_UPDATE_INTERVAL);
			
			
		
		 } 
		 } catch (Exception e) {
			// TODO: handle exception
		 }
	   	
		list.remove(bloom);
		threadList.remove(this);
	}
	public void pauseRunning(){
		pause=true;
	}
	public void resumeRunning(long time){
		pause=false;
		bestTime=bestTime+time;
	}
	public void living(){
		
	}
	public void dead(){
		
	}
	public void isLink(){
		
	}
    private Bitmap zoomIn(Bitmap input,float scale){
		
		Matrix matrix = new Matrix();
	
        matrix.postScale(scale, scale);
            
        Bitmap output = Bitmap.createScaledBitmap(input, (int)(input.getWidth()*scale), (int)(input.getHeight()*scale), false);
        
        
        return output;
	}
}
